// 雪花动效模块（优化版）
function initSnowEffect(canvas, options = {}) {
    // 获取2D渲染上下文
    const ctx = canvas.getContext('2d');
    const snowflakes = [];
    const snowflakeCount = 150; // 增加雪花数量，提升视觉效果
    let time = 0;
    
    // 配置选项
    const config = {
        backgroundType: options.backgroundType || 'gradient', // 'gradient' 或 'solid'
        solidColor: options.solidColor || '#1a1a2e', // 默认深蓝色背景
        ...options
    };
    
    // 雪花形状类型
    const snowflakeTypes = [
        // 六边形
        function(ctx, size) {
            ctx.beginPath();
            for (let i = 0; i < 6; i++) {
                const angle = (Math.PI / 3) * i;
                const wx = Math.cos(angle) * size;
                const wy = Math.sin(angle) * size;
                if (i === 0) {
                    ctx.moveTo(wx, wy);
                } else {
                    ctx.lineTo(wx, wy);
                }
            }
            ctx.closePath();
        },
        // 星形
        function(ctx, size) {
            const spikes = 6;
            const outerRadius = size;
            const innerRadius = size * 0.5;
            
            ctx.beginPath();
            for (let i = 0; i < spikes * 2; i++) {
                const radius = i % 2 === 0 ? outerRadius : innerRadius;
                const angle = (Math.PI / spikes) * i - Math.PI / 2;
                const x = Math.cos(angle) * radius;
                const y = Math.sin(angle) * radius;
                
                if (i === 0) {
                    ctx.moveTo(x, y);
                } else {
                    ctx.lineTo(x, y);
                }
            }
            ctx.closePath();
        },
        // 点状雪花
        function(ctx, size) {
            ctx.beginPath();
            ctx.arc(0, 0, size * 0.8, 0, Math.PI * 2);
            ctx.closePath();
        },
        // 十字形
        function(ctx, size) {
            // 水平线
            ctx.beginPath();
            ctx.moveTo(-size, 0);
            ctx.lineTo(size, 0);
            // 垂直线
            ctx.moveTo(0, -size);
            ctx.lineTo(0, size);
            // 对角线
            ctx.moveTo(-size * 0.7, -size * 0.7);
            ctx.lineTo(size * 0.7, size * 0.7);
            ctx.moveTo(-size * 0.7, size * 0.7);
            ctx.lineTo(size * 0.7, -size * 0.7);
        }
    ];
    
    // 创建雪花
    for (let i = 0; i < snowflakeCount; i++) {
        const size = Math.random() * 3 + 1;
        snowflakes.push({
            x: Math.random() * canvas.width,
            y: Math.random() * canvas.height,
            size: size,
            speed: (size * 0.2) + Math.random() * 1.5, // 大小影响速度
            opacity: Math.random() * 0.7 + 0.3,
            angle: Math.random() * Math.PI * 2,
            swaySpeed: Math.random() * 0.05 + 0.01,
            rotation: Math.random() * Math.PI * 2,
            rotationSpeed: (Math.random() - 0.5) * 0.03,
            shapeType: Math.floor(Math.random() * snowflakeTypes.length),
            windInfluence: Math.random() * 0.8 + 0.2 // 风力影响因子
        });
    }
    
    // 设置背景
    function drawBackground() {
        if (config.backgroundType === 'solid') {
            // 纯色背景
            ctx.fillStyle = config.solidColor;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        } else {
            // 多彩渐变背景
            const gradient = ctx.createRadialGradient(
                canvas.width / 2, canvas.height / 2, 0,
                canvas.width / 2, canvas.height / 2, Math.max(canvas.width, canvas.height)
            );
            const timeFactor = time * 0.0005;
            const hue = 220 + Math.sin(timeFactor) * 20;
            
            gradient.addColorStop(0, `hsl(${hue}, 30%, 8%)`);
            gradient.addColorStop(0.6, `hsl(${(hue + 30) % 360}, 40%, 5%)`);
            gradient.addColorStop(1, `hsl(${(hue + 60) % 360}, 35%, 3%)`);
            
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        }
    }
    
    // 绘制雪花
    function drawSnowflake(snowflake) {
        ctx.save();
        ctx.globalAlpha = snowflake.opacity;
        ctx.fillStyle = 'white';
        ctx.strokeStyle = 'white';
        ctx.lineWidth = 0.5;
        ctx.translate(snowflake.x, snowflake.y);
        ctx.rotate(snowflake.rotation);
        
        // 使用雪花类型函数绘制不同形状
        snowflakeTypes[snowflake.shapeType](ctx, snowflake.size);
        
        // 填充并描边以增强可见度
        ctx.fill();
        ctx.stroke();
        
        // 为较大的雪花添加光晕效果
        if (snowflake.size > 2) {
            ctx.globalAlpha = snowflake.opacity * 0.3;
            ctx.beginPath();
            ctx.arc(0, 0, snowflake.size * 1.5, 0, Math.PI * 2);
            ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
            ctx.fill();
        }
        
        ctx.restore();
    }
    
    // 动画循环
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        // 绘制背景
        drawBackground();
        
        // 计算风力影响（随时间变化）
        const windStrength = 0.5 + Math.sin(time * 0.002) * 0.5;
        
        snowflakes.forEach(snowflake => {
            // 更新雪花位置 - 添加风力影响和更自然的飘落轨迹
            snowflake.y += snowflake.speed;
            snowflake.angle += snowflake.swaySpeed;
            snowflake.x += Math.sin(snowflake.angle) * 0.3 + 
                          Math.sin(time * 0.001 + snowflake.y * 0.01) * snowflake.windInfluence * windStrength;
            
            // 更新旋转
            snowflake.rotation += snowflake.rotationSpeed;
            
            // 轻微的闪烁效果
            snowflake.opacity = Math.max(0.3, Math.min(1, 
                snowflake.opacity + (Math.random() - 0.5) * 0.02));
            
            // 绘制雪花
            drawSnowflake(snowflake);
            
            // 如果雪花超出画布，重新放置到顶部
            if (snowflake.y > canvas.height + 10) {
                resetSnowflake(snowflake);
            }
            
            // 左右边界检测
            if (snowflake.x < -20) {
                snowflake.x = canvas.width + 20;
            } else if (snowflake.x > canvas.width + 20) {
                snowflake.x = -20;
            }
        });
        
        time++;
        requestAnimationFrame(animate);
    }
    
    // 重置雪花属性
    function resetSnowflake(snowflake) {
        snowflake.y = -10;
        snowflake.x = Math.random() * canvas.width;
        snowflake.size = Math.random() * 3 + 1;
        snowflake.speed = (snowflake.size * 0.2) + Math.random() * 1.5;
        snowflake.opacity = Math.random() * 0.7 + 0.3;
        snowflake.shapeType = Math.floor(Math.random() * snowflakeTypes.length);
        snowflake.rotation = Math.random() * Math.PI * 2;
        snowflake.rotationSpeed = (Math.random() - 0.5) * 0.03;
    }
    
    animate();
}

// 暴露函数到全局作用域
window.initSnowEffect = initSnowEffect;